package com.yh.servlet.admin;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yh.entity.Zhanfeng;
import com.yh.service.UserService;
import com.yh.service.impl.UserServiceImpl;

import cn.hutool.core.bean.BeanUtil;

@WebServlet("/admin/user-update.let")
public class UserUpdateServlet extends HttpServlet {
	
	private Logger log = LoggerFactory.getLogger(UserUpdateServlet.class);
	private UserService userService = new UserServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		
		Zhanfeng user = new Zhanfeng();
		Zhanfeng oldUser = new Zhanfeng();
		BeanUtil.copyProperties(userService.Get(Integer.valueOf(req.getParameter("userId"))), oldUser);
		
		try {
			user.setZfId(Integer.valueOf(req.getParameter("userId")));
			user.setZfName(req.getParameter("userName"));
			user.setZfSex(req.getParameter("userSex"));
//			System.out.println(req.getParameter("userSex"));
			log.debug("userSex:{}",req.getParameter("userSex"));
			user.setZfTime(new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("userTime")));
			user.setZfAge(Integer.valueOf(req.getParameter("userAge")).equals("")?oldUser.getZfAge():Integer.valueOf(req.getParameter("userAge")));
			user.setZfPassword(oldUser.getZfPassword());
			
			userService.update(user);
			
			req.setAttribute("msg", "修改用户成功");
			req.setAttribute("href","admin/user-list.let");
		} catch (ParseException e) {
			e.printStackTrace();
			req.setAttribute("error", "修改用户失败:"+e.getMessage());
		}
		
		req.getRequestDispatcher("/admin/msg.jsp").forward(req, resp);
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException ,IOException {
		doPost(req, resp);
	}
}
